//
//  CMFReSetPasswordCtrl.m
//  CreditManger
//
//  Created by 于会海 on 15/11/2.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMFReSetPasswordCtrl.h"
#import "CMResetUserPwdParam.h"
#import "CMResetUserPwdParser.h"

#import "CMLoginCtrl.h"
#import "CMNavigationController.h"
#import "AppDelegate+NotificationCenter.h"
#import "CMMySettingCtrl.h"

@interface CMFReSetPasswordCtrl ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *rePassword;
@property (weak, nonatomic) IBOutlet UIButton *doBtn;

- (IBAction)doBtnClicked:(UIButton *)sender;
@end

@implementation CMFReSetPasswordCtrl
#define LCAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)buildLayout{
    self.title = @"设置密码";
    self.password.delegate = self;
    self.rePassword.delegate = self;
    self.doBtn.layer.cornerRadius = 4.0;
}

#pragma textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"])
    {
        return YES;
    }
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.password == textField || self.rePassword == textField) {
        if ([toBeString length] > 17)
        {
            textField.text = [toBeString substringToIndex:18];
            [textField resignFirstResponder];
            return NO;
        }
    }
    return YES;
}

- (void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doBtnClicked:(UIButton *)sender {
    if([self.password.text length] < 6){
        UIAlertView* passwordAlert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"密码不能低于6位" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [passwordAlert show];
        return;
    }
    if ([self.password.text isEqualToString:self.rePassword.text]) {
        CMResetUserPwdParam* param = [[CMResetUserPwdParam alloc] init];
        param.mobile = self.phoneNum;
        param.password = self.password.text;
        CMResetUserPwdParser* parser = [[CMResetUserPwdParser alloc] init];
        
        [NetCenter postDataFromURL:Method_resetUserPwd params:param parser:parser result:^(ParserResult *myResult) {
            if (myResult.code == 0) {
                DISSMISS_ERR(myResult.message);
                //延时进入login页面
                dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
                dispatch_after(when, dispatch_get_main_queue(), ^{
                    if (self.isModify)
                    {
                        for (UIViewController *vc in self.navigationController.viewControllers)
                        {
                            if ([vc isKindOfClass:[CMMySettingCtrl class]])
                            {
                                [self.navigationController popToViewController:vc animated:YES];
                            }
                        }
                    }
                    else
                    {
                        CMLoginCtrl *login = [[CMLoginCtrl alloc]init];
                        CMNavigationController *nav = [[CMNavigationController alloc]initWithRootViewController:login];
                        [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                    }
                    
                });
            }else if(myResult.code == 1){
                 DISSMISS_ERR(myResult.message);
            }
        } failure:^(NSError *error) {
            DISSMISS_ERR(error.description);
        }];
    }else{
        UIAlertView* passwordAlert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"两次输入不一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [passwordAlert show];
        return;
    } 
}
@end
